package ext.acae2.dms.web.output;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import wt.log4j.LogR;
import ext.acae2.dms.bean.DocReferenceLinkBean;
import ext.acae2.dms.bean.DocumentBean;
import ext.acae2.dms.bean.PartBean;
import ext.acae2.dms.bean.PartUsageLinkBean;
import ext.acae2.util.ParameterUtil;

public class ToxmlProcessor {

	private static final Logger LOGGER = LogR.getLogger(ToxmlProcessor.class
			.getName());

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean toxml(Map<String, List> map, String downloadPath,
			String fileName) {
		LOGGER.debug("> Start toxml");

		List<PartBean> partList = map.get(ParameterUtil.PARTBEAN);

		List<DocumentBean> docList = map.get(ParameterUtil.DOCUMENTBEAN);

		List<PartUsageLinkBean> partLinkList = map
				.get(ParameterUtil.PARTUSAGELINKBEAN);

		List<DocReferenceLinkBean> docLinkList = map
				.get(ParameterUtil.DOCREFERENCELINKBEAN);

		String xmlFile = downloadPath + File.separator + fileName + ".xml";
		Document document = DocumentHelper.createDocument();

		Element productStructureElement = document
				.addElement("PRODUCTSTRUCTURE");

		if (partList != null) {
			for (int i = 0; i < partList.size(); i++) {
				Element partElement = productStructureElement
						.addElement("PART");
				partElement.addAttribute(ParameterUtil.TYPE, partList.get(i)
						.getPARTTYPE());
				partElement.addAttribute(ParameterUtil.NUMBER, partList.get(i)
						.getNumber());
				partElement.addAttribute(ParameterUtil.NAME, partList.get(i)
						.getName());
				partElement.addAttribute(ParameterUtil.VERSION, partList.get(i)
						.getVersion());
				
				partElement.addAttribute("LIFECYCLESTATE", "RELEASED");
				
				Element partAttributesElement = partElement
						.addElement("ATTRIBUTES");

				Set<Map.Entry<String, String>> set = partList.get(i).getMap()
						.entrySet();
				for (Iterator<Map.Entry<String, String>> it = set.iterator(); it
						.hasNext();) {
					Element partAttributeElement = partAttributesElement
							.addElement("ATTRIBUTE");
					Map.Entry<String, String> entry = (Map.Entry<String, String>) it
							.next();
					partAttributeElement.addAttribute(ParameterUtil.NAME,
							entry.getKey());
					partAttributeElement.addAttribute(ParameterUtil.VALUE,
							entry.getValue());
					LOGGER.debug("@@@@@@" + entry.getKey() + "--->"
							+ entry.getValue() + "@@@@@@");
				}

				Element cadFilesElement = partElement.addElement("CADFILES");
				if (partList.get(i).getEpmDocList().size() == 0) {
					Element cadFileElement = cadFilesElement
							.addElement("CADFILE");
					cadFileElement.addAttribute(ParameterUtil.NUMBER, "");
					cadFileElement.addAttribute(ParameterUtil.NAME, "");
					cadFileElement.addAttribute(ParameterUtil.VERSION, "");
					cadFileElement.addAttribute(ParameterUtil.TYPE, "");
					cadFileElement.setText("");

				} else {
					for (int j = 0; j < partList.get(i).getEpmDocList().size(); j++) {
						Element cadFileElement = cadFilesElement
								.addElement("CADFILE");
						cadFileElement.addAttribute(ParameterUtil.NUMBER,
								partList.get(i).getEpmDocList().get(j)
										.getNumber());
						cadFileElement.addAttribute(ParameterUtil.NAME,
								partList.get(i).getEpmDocList().get(j)
										.getName());
						cadFileElement.addAttribute(ParameterUtil.VERSION,
								partList.get(i).getEpmDocList().get(j)
										.getVersion());
						cadFileElement.addAttribute(ParameterUtil.TYPE,
								partList.get(i).getEpmDocList().get(j)
										.getLocationRef().split(",")[1]);
						cadFileElement.setText(partList.get(i).getEpmDocList()
								.get(j).getLocationRef().split(",")[0]);
					}
				}

				Element jtFilesElement = partElement.addElement("JTFILES");
				if (partList.get(i).getJtList().size() == 0) {
					Element jtFileElement = jtFilesElement.addElement("JTFILE");
					jtFileElement.addAttribute(ParameterUtil.NUMBER, "");
					jtFileElement.addAttribute(ParameterUtil.NAME, "");
					jtFileElement.addAttribute(ParameterUtil.TYPE, "");
					jtFileElement.addAttribute(ParameterUtil.VERSION, "");
					jtFileElement.setText("");

				} else {
					for (int j = 0; j < partList.get(i).getJtList().size(); j++) {
						Element jtFileElement = jtFilesElement
								.addElement("JTFILE");
						jtFileElement.addAttribute(ParameterUtil.NUMBER,
								partList.get(i).getJtList().get(j).getId());
						jtFileElement.addAttribute(ParameterUtil.NAME, partList
								.get(i).getJtList().get(j).getName());
						jtFileElement.addAttribute(ParameterUtil.TYPE, partList
								.get(i).getJtList().get(j).getType());
						jtFileElement
								.addAttribute(ParameterUtil.VERSION, partList
										.get(i).getJtList().get(j).getVersion());
						jtFileElement.setText(partList.get(i).getJtList()
								.get(j).getLocationRef());
					}
				}
			}
		}

		if (docList != null) {
			for (int i = 0; i < docList.size(); i++) {
				Element documentElement = productStructureElement
						.addElement("DOCUMENT");
				documentElement.addAttribute(ParameterUtil.TYPE, docList.get(i)
						.getDOCUMENTTYPE());
				documentElement.addAttribute(ParameterUtil.NUMBER,
						docList.get(i).getNumber());
				documentElement.addAttribute(ParameterUtil.NAME, docList.get(i)
						.getName());
				documentElement.addAttribute(ParameterUtil.TITLE, docList
						.get(i).getTITLE());
				documentElement.addAttribute(ParameterUtil.DESCRIPTION, docList
						.get(i).getDescription());
				documentElement.addAttribute(ParameterUtil.LIFECYCLESTATE,
						docList.get(i).getState());
				documentElement.addAttribute(ParameterUtil.VERSION, docList
						.get(i).getVersion());

				documentElement.addAttribute("LIFECYCLESTATE", "RELEASED");
				
				Element documentAttributesElement = documentElement
						.addElement("ATTRIBUTES");

				Set<Map.Entry<String, String>> set = docList.get(i).getMap()
						.entrySet();
				for (Iterator<Map.Entry<String, String>> it = set.iterator(); it
						.hasNext();) {
					Element documentAttributeElement = documentAttributesElement
							.addElement("ATTRIBUTE");
					Map.Entry<String, String> entry = (Map.Entry<String, String>) it
							.next();
					documentAttributeElement.addAttribute(ParameterUtil.NAME,
							entry.getKey());
					documentAttributeElement.addAttribute(ParameterUtil.VALUE,
							entry.getValue());
					LOGGER.debug("@@@@@@" + entry.getKey() + "--->"
							+ entry.getValue() + "@@@@@@");
				}

				Element contentsElement = documentElement
						.addElement("CONTENTS");

				for (int j = 0; j < docList.get(i).getLocationRefList().size(); j++) {
					Element contentElement = contentsElement
							.addElement("CONTENT");
					contentElement.addAttribute(ParameterUtil.NUMBER, docList.get(i).getNumber());
					contentElement.addAttribute(ParameterUtil.VERSION, docList
							.get(i).getVersion());
					contentElement.addAttribute(ParameterUtil.NAME,docList.get(i)
							.getName());
					contentElement.addAttribute(ParameterUtil.TYPE, docList
							.get(i).getLocationRefList().get(j).split(",")[1]);
					contentElement.setText(docList.get(i).getLocationRefList()
							.get(j).split(",")[0]);
				}

			}
		}

		if (docLinkList != null) {
			for (int i = 0; i < docLinkList.size(); i++) {
				Element partDocLinkElement = productStructureElement
						.addElement("PART_DOC_LINK");
				partDocLinkElement.addAttribute(ParameterUtil.LINKEDDOC_NUMBER,
						docLinkList.get(i).getDocument().getNumber());
				partDocLinkElement.addAttribute(
						ParameterUtil.LINKEDDOC_VERSION, docLinkList.get(i)
								.getDocument().getVersion());
				partDocLinkElement.addAttribute(
						ParameterUtil.LINKEDPART_NUMBER, docLinkList.get(i)
								.getPart().getNumber());
				partDocLinkElement.addAttribute(
						ParameterUtil.LINKEDPART_VERSION, docLinkList.get(i)
								.getPart().getVersion());
			}
		}

		if (partLinkList != null) {
			for (int i = 0; i < partLinkList.size(); i++) {
				Element assemblyElement = productStructureElement
						.addElement("ASSEMBLY");
				assemblyElement.addAttribute(ParameterUtil.PARENTPART_NUMBER,
						partLinkList.get(i).getParentPart().getNumber());
				assemblyElement.addAttribute(ParameterUtil.PARENTPART_VERSION,
						partLinkList.get(i).getParentPart().getVersion());
				assemblyElement.addAttribute(ParameterUtil.CHILDPART_NUMBER,
						partLinkList.get(i).getChildPart().getNumber());
				assemblyElement.addAttribute(ParameterUtil.CHILDPART_VERSION,
						partLinkList.get(i).getChildPart().getVersion());
				assemblyElement.addAttribute(ParameterUtil.CHILDPART_QUANTITY,
						partLinkList.get(i).getSingleAmout() + "");
			}
		}

		try {

			FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);

			OutputFormat outFormat = OutputFormat.createPrettyPrint();
			outFormat.setEncoding("UTF-8");
			outFormat.setTrimText(false);

			XMLWriter writer = new XMLWriter(fileOutputStream, outFormat);

			document.setXMLEncoding("UTF-8");
			writer.write(document);
			writer.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

	}
}